#ifndef SETTINGSAREA_H
#define SETTINGSAREA_H

#include "AiServiceSettings.h"
#include "Embedding.h"
#include "ImageChatService.h"
#include "PathSettings.h"
#include "UpdateFunctions.h"
#include <QComboBox>
#include <QDoubleSpinBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QScrollArea>
#include <QSpinBox>
#include <QTabWidget>
#include <QTextEdit>
#include <QWidget>

class SettingsArea : public QWidget
{
    Q_OBJECT

public:
    explicit SettingsArea(QWidget *parent = nullptr);

signals:
    void imageListUpdated(const QVector<FileInfo> &imageList);

private:
    void setupUI();
    void connectSignals();
    void log(const QString &message);

    void clearAndScanFolder();
    void appendScanFolder();
    void searchByContent();
    void searchByType();
    void searchByFace();
    void searchByKeywords();
    void openSettingsDialog();
    void selectKnownFacesDir();
    void selectScanFolder();
    void openPathSettingsDialog();

    AIServiceSettings m_aiSettings;
    UpdateFunctions m_updateFunctions;
    ImageChatService m_imageChatService;
    Embedding m_embedding;

    QPushButton *m_settingsButton;
    QPushButton *m_clearAndScanFolderButton;
    QPushButton *m_appendScanFolderButton;
    QLineEdit *m_contentSearchInput;
    QPushButton *m_contentSearchButton;
    QSpinBox *m_contentSearchResultsSpinBox;
    QLineEdit *m_typeSearchInput;
    QPushButton *m_typeSearchButton;
    QLineEdit *m_faceSearchInput;
    QPushButton *m_faceSearchButton;
    QLineEdit *m_keywordSearchInput;
    QPushButton *m_keywordSearchButton;
    QTextEdit *m_logOutput;

    PathSettings *m_pathSettings;
    QPushButton *m_modifyPathButton;

    QComboBox *m_faceSelectComboBox;
    QPushButton *m_faceSelectButton;

    void updateFaceList();
    void searchSelectedFace();

    QComboBox *m_typeSelectComboBox;
    QPushButton *m_typeSelectButton;

    void searchSelectedType();

    QPushButton *m_recognizeFacesButton;
    QPushButton *m_regenerateTypeButton;
    QPushButton *m_regenerateDescriptionButton;
    QPushButton *m_regenerateEmbeddingButton;

    void recognizeFaces();
    void regenerateType();
    void regenerateDescription();
    void regenerateEmbedding();

    bool confirmAction(const QString &message);

    QPushButton *m_imageSearchSelectButton;
    QPushButton *m_imageSearchButton;

    void selectImageForSearch();
    void searchByImage();

    QLabel *m_imagePreviewLabel;
    QString m_selectedImagePath;

    void updateImagePreview(const QString &imagePath);

    QString m_lastProcessedImagePath;
    QVector<float> m_lastQueryEmbedding;

    // UI 创建辅助函数
    void createBasicTab(QTabWidget *tabWidget);
    void createScanTab(QTabWidget *tabWidget);
    void createSearchTab(QTabWidget *tabWidget);
    void createLogArea(QVBoxLayout *mainLayout);

    // 搜索选项卡辅助函数
    void createContentSearchTab(QTabWidget *searchTabWidget);
    void createTypeSearchTab(QTabWidget *searchTabWidget);
    void createFaceSearchTab(QTabWidget *searchTabWidget);
    void createKeywordSearchTab(QTabWidget *searchTabWidget);

    // 内容搜索选项卡组件创建
    void setupResultsCountArea(QVBoxLayout *contentSearchLayout, QWidget *parent);
    void setupContentSearchArea(QVBoxLayout *contentSearchLayout, QWidget *parent);
    void setupImageSearchArea(QVBoxLayout *contentSearchLayout, QWidget *parent);

    // ... 在private部分添加以下成员变量
    QString m_lastContentQuery;
    QVector<float> m_lastContentEmbedding;

    QPushButton *m_rescanFolderButton;

    void rescanFolder();

    QPixmap m_originalPreviewPixmap;

    QPushButton *m_recognizeAllFacesButton;

    void recognizeAllFaces();

    QPushButton *m_refreshFaceListButton;

    QDoubleSpinBox *m_faceRecognitionThresholdSpinBox;

    QLabel *m_facePreviewLabel;
    QPixmap m_originalFacePreviewPixmap;

    void updateFacePreview(const QString &faceName);

    void safeUpdateFaceList();
};

#endif // SETTINGSAREA_H
